home *** CD-ROM | disk | FTP | other *** search
- /**************************************************************************
- *
- * @@@BUILDINFO@@@ 12document.jsx 1.0.0.59 05-Mar-2005
- * Copyright 2005 Adobe Systems Incorporated
- * All Rights Reserved.
- *
- * NOTICE: All information contained herein is, and remains the property of
- * Adobe Systems Incorporated and its suppliers, if any. The intellectual
- * and technical concepts contained herein are proprietary to Adobe Systems
- * Incorporated and its suppliers and may be covered by U.S. and Foreign
- * Patents,patents in process,and are protected by trade secret or copyright
- * law. Dissemination of this information or reproduction of this material
- * is strictly forbidden unless prior written permission is obtained from
- * Adobe Systems Incorporated.
- **************************************************************************/
-
- // This file contains add-on functionality for the built-in Document object.
-
- // Documents may have the following optional properties:
- // status - one of the following:
- // exec - executable
- // noexec - not executable
- // active - has been executed, waiting for callbacks in the target
- // dynamic - no script ID, came from target, not saveable & restartable
- // tempBP - a line number for a temporary breakpoint
-
- // The script ID is one of the following:
- // (name) - if the document is newly created
- // /path - if the document has been saved into a file
- // id - if the target app has supplied the ID
-
- // Callback for a document after it has been created, but before it contains any text.
-
- Document.prototype.onCreate = function()
- {
- // the default
- this.status = "exec";
- // the last number of BPs sent to the target
- this.lastBPCount = 0;
- // TIGER (Mac OS 10.4) COLOR PROBLEM FIX: draw unbuffered on Tiger
- var os = $.os;
- if (os.indexOf ("Macintosh OS ") == 0)
- {
- if (parseFloat (os.substr (13)) > 10.3)
- this.__flags__ |= 2;
- }
- // TIGER (Mac OS 10.4) COLOR PROBLEM FIX END
- if (documents.length == 1)
- debugMenu.reflectState();
- }
-
- // Callback for a document after it has been loaded from a disk file.
-
- Document.prototype.onLoad = function()
- {
- this.addToRecentFiles();
- }
-
- // Callback for a document when it is activated or deactivated.
-
- Document.prototype.onActivate = function (on)
- {
- // Update the Breakpoints pane
- if (on)
- window.breakpoints.update();
- // Don't invalidate on deactivate, because deactivation occurs whenever
- // current doc window loses focus.
- }
-
- // Callback for documents to set the help tip by context
-
- Document.prototype.onMouseOver = function (line, text)
- {
- if (currentDebugger && prefs.helpTips && prefs.dynamicHelp)
- currentDebugger.getHelpTip (this, line, text);
- else
- this.helpTip = "";
- }
-
- // Called when the editor is about to be modified. Return false to deny the request.
-
- Document.prototype.onModify = function()
- {
- var ok = true;
- // If we are debugging, we need to ask whether to stop
- if (currentDebugger && currentDebugger.isActive())
- {
- if (!queryBox ("$$$/ESToolkit/Alerts/StopDebugging=Stop debugging?"))
- return false;
- // return false if there is a different text displayed
- if (this.savedText)
- ok = false;
- currentDebugger.stop();
- }
- return ok;
- }
-
- // Called whenever the Toolkit detects a change on disk that affects a loaded file.
- // Return false to tell the Toolkit that the disk image is different from the Toolkit's data.
-
- Document.prototype.onModifiedFromOutside = function()
- {
- var msg = "$$$/ESToolkit/Alerts/Modified1=File %1^nhas been changed outside of the Toolkit.^nDo you want to reload it?";
- if (this.modified)
- msg = "$$$/ESToolkit/Alerts/Modified2=File %1^nhas been changed outside of the Toolkit.^nDo you want to reload it and lose all your changes?";
- var reload = prefs.autoReload || queryBox (msg, this.path.fsName);
- if (reload)
- {
- var f = this.path;
- if (f.open())
- {
- this.text = f.read();
- f.close();
- }
- reload = (f.status == "");
- }
- return reload;
- }
-
- // Called when the line count of the editor has changed.
-
- Document.prototype.onLineNumbersChanged = function()
- {
- // Update the Breakpoints pane, because the change in line #s
- // may mean a line with a breakpoint has been deleted, or
- // lines were inserted, changing the line numbers on existing BPs
- window.breakpoints.update();
- }
-
- // Callback for documents to set, disable, or clear a breakpoint
-
- Document.prototype.onLineClick = function (line, count)
- {
- // ignore the click count for now
- this.toggleBP (line);
- }
-
- // When a document is about to be saved, send the target a PutScript message
- // if there is a target and engine defined, and if the script ID is not a file.
- // Return true if a JS save operation was committed or initiated.
-
- Document.prototype.onSave = function()
- {
- return window.scripts.storeScript (this);
- }
-
- // Callback notification when a document has been saved.
-
- Document.prototype.onSaved = function()
- {
- this.addToRecentFiles();
- }
-
- // Ask if it is OK to close this document.
-
- Document.prototype.queryClose = function()
- {
- // if this doc is being debugged, ask to stop debugging
- if (currentDebugger && currentDebugger.isActive() && currentDebugger.document == this)
- return Debugger.queryExit();
- else
- return true;
- }
-
- // When a document is about to close, deselect the Scripts pane.
-
- Document.prototype.onClose = function()
- {
- window.scripts.deselect();
- currentDebugger.closeDocument (this);
- if (documents.length == 1 && !currentDebugger.isActive())
- // If there are no documents left, and no active debugger, disable debugging
- // If there is an active debugger
- debugMenu.disable();
- // Update the Breakpoints pane
- window.breakpoints.invalidate();
- }
-
- // Add this document to the Recent Files list.
-
- Document.prototype.addToRecentFiles = function()
- {
- // If the doc is a file, append it to the Recent Files list
- var f = new File (this.path);
- if (f.exists)
- {
- var path = f.absoluteURI;
- for (var i = 0; i < prefs.recentFiles.length; i++)
- {
- if (prefs.recentFiles [i] == path)
- {
- // remove if present
- prefs.recentFiles.splice (i, 1);
- break;
- }
- }
- if (prefs.recentFiles.length == 9)
- prefs.recentFiles.pop();
- // insert the new name at the top
- prefs.recentFiles.splice (0, 0, path);
- }
- }
-
- ///////////////////////////////////////////////////////////////
-
- // Breakpoints.
-
- // Get a list of all breakpoints. This call returns an array if breakpoints
- // containing objects. Each objects contains a line property (number, zero-based),
- // a enabled property (true/false), and a condition property (string) if there
- // is a condition attached.
-
- Document.prototype.getAllBP = function()
- {
- var bps = [];
- var lines = this.getFlaggedLines();
- for (var i = 0; i < lines.length; i++)
- {
- var line = lines [i];
- var flags = this.lineFlags [line] & Debugger.BP_MASK;
- var obj = { line:line, enabled:((flags & Debugger.BP_ENABLED_MASK) != 0) };
- var cond = this.lineData [line];
- if (cond)
- obj.condition = cond;
- bps.push (obj);
- }
- return bps;
- }
-
- // Is there a breakpoint at tyhe given line?
-
- Document.prototype.isBP = function (line)
- {
- return ((this.lineFlags [line] & Debugger.BP_MASK) != 0);
- }
-
- // Restore a breakpoint; used when loading the prefs
-
- Document.prototype.restoreBP = function (line, enabled, cond)
- {
- var flags;
- if (cond)
- flags = enabled ? Debugger.BP_COND_ENABLED : Debugger.BP_COND_DISABLED;
- else
- flags = enabled ? Debugger.BP_ENABLED : Debugger.BP_DISABLED;
- this.lineFlags [line] = flags;
- if (cond)
- this.lineData [line] = cond;
- this.sendBP();
- }
-
- // Delete a breakpoint including its condition.
-
- Document.prototype.clearBP = function (line)
- {
- if (this.lineFlags [line] & Debugger.BP_MASK)
- {
- this.lineFlags [line] &= ~Debugger.BP_MASK;
- this.lineData [line] = null;
- // Update the Breakpoints pane
- window.breakpoints.update();
- this.sendBP();
- }
- }
-
- // Toggle a breakpoint from off to enabled to disabled.
-
- Document.prototype.toggleBP = function (line)
- {
- var oldFlags = this.lineFlags [line];
- var cond = this.lineData [line];
- var flags = oldFlags & ~Debugger.BP_MASK;
- switch (oldFlags & Debugger.BP_MASK)
- {
- case 0:
- this.lineFlags [line] = flags | (cond ? Debugger.BP_COND_ENABLED : Debugger.BP_ENABLED);
- break;
- case Debugger.BP_COND_ENABLED:
- case Debugger.BP_ENABLED:
- this.lineFlags [line] = flags | (cond ? Debugger.BP_COND_DISABLED : Debugger.BP_DISABLED);
- break;
- default:
- this.lineFlags [line] = flags;
- }
- // Update the Breakpoints pane
- window.breakpoints.update();
- this.sendBP();
- }
-
- // Remove all breakpoints and conditions.
-
- Document.prototype.clearAllBP = function()
- {
- var lines = this.getFlaggedLines();
- for (var i = 0; i < lines.length; i++)
- {
- var line = lines [i];
- this.lineFlags [line] = this.lineFlags [line] & ~Debugger.BP_MASK;
- this.lineData [line] = null;
- }
- if (lines.length)
- {
- // Update the Breakpoints pane
- window.breakpoints.update();
- this.sendBP();
- }
- }
-
- // Send an updated list of breakpoints to the target if the target is running.
-
- Document.prototype.sendBP = function()
- {
- if (currentDebugger && currentDebugger.state != Debugger.INACTIVE)
- {
- var bt = BridgeTalk.create (currentDebugger.target, "Breakpoints");
- bt.headers.Engine = currentDebugger.engine;
- bt.headers.ScriptID = this.scriptID;
- this.attachBP (bt);
- bt.safeSend();
- }
- }
-
- // Append all breakpoints for the document to the supplied BT message.
- // All breakpoints are stored in the header as BPx, where x counts from 1.
- // The BP info is the line number (0-based), a space, and a condition.
- // The header BPCount takes the number of BPs. If document.tempBP is defined, also
- // define a temporary BP at that line
-
- Document.prototype.attachBP = function (bt)
- {
- var lineFlags = this.lineFlags;
- var bpCount = 0;
- var tempBP = this.tempBP;
- delete this.tempBP;
-
- var lines = this.getFlaggedLines();
- for (var i = 0; i < lines.length; i++)
- {
- var line = lines [i];
- var flags = lineFlags [line] & Debugger.BP_MASK;
- if (flags == Debugger.BP_ENABLED || flags == Debugger.BP_COND_ENABLED)
- {
- // the temp BP matches a real BP, so forget about it
- if (line == tempBP)
- tempBP = undefined;
- var text = line + ",true";
- var cond = this.lineData [line];
- if (cond)
- text += "," + bt.encode (cond);
- bt.headers ["BP"+ ++bpCount] = text;
- }
- }
- // append a temp BP if given
- if (tempBP != undefined)
- bt.headers ["BP"+ ++bpCount] = tempBP + ",temp";
- if (bpCount != this.lastBPCount)
- bt.headers.BPClear = "1";
- if (bpCount > 0)
- bt.headers.BPCount = bpCount;
- this.lastBPCount = bpCount;
- }
-
- // Return a string containing all breakpoints as needed by the SetBreakpoints message.
- // The body of this message is formatted as follows:
- // script ID
- // line,state[,condition] where state is true,false,temp
- // ...
- // <newline>
-
- Document.prototype.createBPList = function()
- {
- var lineFlags = this.lineFlags;
- var bpCount = 0;
- var tempBP = this.tempBP;
- delete this.tempBP;
- var reply = "";
-
- var lines = this.getFlaggedLines();
- for (var i = 0; i < lines.length; i++)
- {
- var line = lines [i];
- var flags = lineFlags [line] & Debugger.BP_MASK;
- if (flags == Debugger.BP_ENABLED || flags == Debugger.BP_COND_ENABLED)
- {
- // the temp BP matches a real BP, so forget about it
- if (line == tempBP)
- tempBP = undefined;
- var text = line + ",true";
- var cond = this.lineData [line];
- if (cond)
- text += "," + bt.encode (cond);
- reply += text + '\n';
- bpCount++;
- }
- }
- // append a temp BP if given
- if (tempBP != undefined)
- reply += tempBP + ",temp\n", bpCount++;
- if (bpCount)
- reply = this.scriptID + '\n' + reply + '\n';
- return reply;
- }
-
- // Evaluate the body of a Breakpoints message sent by the target.
- // This message contains a list of altered breakpoints, two numbers
- // on each line. If the breakpoint could not be set at all, the second
- // number is < 0.
-
- Document.prototype.updateBP = function (bt)
- {
- var reply = bt.splitBody();
- for (var i = 0; i < reply.length; i++)
- {
- var oldLine = +reply [i][0];
- var newLine = +reply [i][1];
- var oldBP = this.lineFlags [oldLine] & Debugger.BP_MASK;
- var oldCond = this.lineData [oldLine];
- this.lineFlags [oldLine] &= ~Debugger.BP_MASK;
- this.lineData [oldLine] = null;
- if (newLine >= 0)
- {
- this.lineFlags [newLine] = (this.lineFlags [newLine] & ~Debugger.BP_MASK) | oldBP;
- this.lineData [newLine] = oldCond;
- }
- }
- // If any BP locations were changed, update the Breakpoints pane
- if (reply.length)
- window.breakpoints.update();
- }
-
- // Write all breakpoints to the Prefs file.
-
- Document.prototype.writeBP = function (f)
- {
- var hdrWritten = false;
- var lineFlags = this.lineFlags;
- var lines = this.getFlaggedLines();
- for (var i = 0; i < lines.length; i++)
- {
- var line = lines [i];
- var flags = lineFlags [line] & Debugger.BP_MASK;
- if (flags)
- {
- if (!hdrWritten)
- hdrWritten = true,
- f.write ('\tdoc = documents.find ("' + this.scriptID + '");\n\tif (doc) {\n');
- f.write ('\t\tdoc.restoreBP (' + line + ', ',
- (flags == Debugger.BP_ENABLED || flags == Debugger.BP_COND_ENABLED));
- var cond = this.lineData [line];
- if (cond)
- f.write (", " + cond.toSource());
- f.write (");\n");
- }
- }
- if (hdrWritten)
- f.write ("\t};\n");
- }
-
- Document.prototype.setProfDisplayMode = function (mode)
- {
- this.profileDisplayMode = mode;
- }
-
- // clear the profiler data for this document
-
- Document.prototype.clearProfData = function()
- {
- this.profileData = null;
- }
-
- /////////////////////////////////////////////////////////////////////////
-
- // Documents collection.
-
- // Set the profiler display data mode for all open documents.
-
- documents.setProfDisplayMode = function (mode)
- {
- for (var i = 0; i < documents.length; i++)
- documents [i].profileDisplayMode = mode;
- }
-
- // Clear the code profiler data for all documents.
-
- documents.clearProfData = function()
- {
- for (var i = 0; i < documents.length; i++)
- documents [i].profileData = null;
- }
-
- // Dump the profiler data into a file.
-
- documents.saveProfData = function()
- {
- var f = new File (Folder.current + "/profileData.csv");
- f = f.saveDlg (
- localize ("$$$/ESToolkit/ProfSaveDlg/Title=Save Profiler Data"),
- localize ("$$$/ESToolkit/ProfSaveDlg/Types=CSV (Comma Delimited):*.csv"));
-
- if (f)
- {
- if (f.open ("w"))
- {
- for (var i = 0; i < documents.length; i++)
- {
- var scriptID = documents [i].scriptID;
- if (scriptID[0] == '/' || scriptID [0] == '~')
- {
- var tmp = new File (scriptID);
- scriptID = tmp.fsName;
- }
- var data = documents [i].profileData;
- if (!data)
- continue;
- for (var j = 0; j < data.length; j++)
- {
- var line = data [j];
- f.write (scriptID + ',' + (line.line+1) + ',' + line.time + ',' + line.hits + '\n');
- }
- }
- }
- if (f.error.length || !f.close())
- {
- errorBox ("$$$/ESToolkit/FileDlg/CannotWrite=Cannot write to file %1!", f.fsName);
- f.remove();
- }
- }
- }
-
- // Find a document by script ID.
-
- documents.find = function (scriptID)
- {
- for (var i = 0; i < documents.length; i++)
- {
- if (documents [i].scriptID == scriptID)
- return documents [i];
- }
- return null;
- }
-
- // Send all breakpoints we have so far to the target.
-
- documents.sendBP = function()
- {
- if (currentDebugger)
- {
- var body = "";
- for (var i = 0; i < documents.length; i++)
- body += documents [i].createBPList();
- if (body.length)
- {
- var bt = BridgeTalk.create (currentDebugger.target, "SetBreakpoints");
- bt.headers.Engine = currentDebugger.engine;
- bt.headers.ScriptID = this.scriptID;
- bt.headers.ClearBP = 1;
- bt.body = body;
- bt.onResult = function (bt)
- {
- // interpret all moved breakpoints
- var reply = bt.splitBody();
- var doc = null;
- var bpChanged = false;
- var newDoc = true;
- for (var i = 0; i < reply.length; i++)
- {
- if (newDoc)
- {
- doc = documents.find (reply [i][0]);
- newDoc = false;
- }
- else if (!reply [i].length)
- {
- doc = null;
- newDoc = true;
- }
- if (doc)
- {
- var oldLine = +reply [i][0];
- var newLine = +reply [i][1];
- var oldBP = doc.lineFlags [oldLine] & Debugger.BP_MASK;
- var oldCond = doc.lineData [oldLine];
- doc.lineFlags [oldLine] &= ~Debugger.BP_MASK;
- doc.lineData [oldLine] = null;
- if (newLine >= 0)
- {
- doc.lineFlags [newLine] = (doc.lineFlags [newLine] & ~Debugger.BP_MASK) | oldBP;
- doc.lineData [newLine] = oldCond;
- }
- bpChanged = true;
- }
- }
- // If any BP locations were changed, update the Breakpoints pane
- if (bpChanged)
- window.breakpoints.update();
- this.destroy();
- }
- bt.safeSend();
- }
- }
- }
-
- // Old dummy stuff to make old prefs files work
-
- function Breakpoints (doc)
- {
- this.doc = doc;
- }
-
- Breakpoints.prototype.set = function (line, enabled, cond)
- {
- this.doc.restoreBP (line, enabled, cond);
- }
-